<?php
	$tasks = mysql_connect('localhost','cs4350','!@'); 
	if (!$tasks) { 
		die('Could not connect to MySQL: ' . mysql_error()); 
	}
	if (!mysql_select_db('todo', $tasks)) {
		die('Aw snap, no database named todo...');
	}
	
	$sql = 'SELECT * FROM list WHERE user="'.$_GET["user"].'"';
	$taskResult = mysql_query($sql, $tasks);	
	
	if (!$taskResult) {
		die($sql);
	}
?>
	
<html>
	<head>
		<?php
			echo '<script>';
			echo 'function verifyUser(){';
			echo 'var provided = "'. $_GET["user"] . '";';
			echo 'var login = sessionStorage.getItem("user");';
			echo 'if (login != provided){';
			echo 'window.location = "/index.html"';
			echo '}}';
			echo '</script>';
		?>
		<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
		<script src = "js/geo.js"></script>
		<STYLE type = "text/css">
		<?php
			while ($taskCSS = mysql_fetch_array($taskResult)){
				echo '#task' . $taskCSS['taskID'] . ' {position: absolute; z-index: 20; top: 90px; left: 150px; border-width: 3px; border-style: double; background: white; opacity: 0; padding:5px; float: left; width: 575px;}
				';
				echo '#edit' . $taskCSS['taskID'] . ':target ~ #task' . $taskCSS['taskID'] . ' {z-index: 100; opacity: 1; transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -webkit-transition: opacity 1s ease-in-out;}
				';
				echo '#edit' . $taskCSS['taskID'] . ':target ~ #fade {z-index: 99; background-color: black; transition: background 1s ease-in-out; -moz-transition: background 1s ease-in-out; -webkit-transition: background 1s ease-in-out;}
				';
			}
			echo '</style>';
		?>
		
		<meta content "text/html; charset=utf-8" http-equiv="Content-Type">
		<title>Share My ToDo</title>
		<link rel="stylesheet" type="text/css" href="css/home.css" />
	</head>
	<body>
	<section>
		<a class="hiddenanchor" id="showCreate"></a>
		<a class="hiddenanchor" id="hideCreate"></a>
		<a class="hiddenanchor" id="cancelEdit"></a>
		<?php
		$sql = 'SELECT * FROM list WHERE user="'.$_GET["user"].'"';
		$taskHidden = mysql_query($sql, $tasks);	
		
		if (!$taskHidden) {
			die($sql);
		}
		while ($hidden = mysql_fetch_array($taskHidden)){
			echo '<a class="hiddenanchor" id="edit' . $hidden['taskID'] . '"></a>';
		}
		?>
		<div id="fade"></div>
		<header>
			<?php 
			echo "<h1>" . $_GET["user"] . "'s ToDo</h1>";
			?>
			<a id = "logout" href="/index.html">Logout</a></br><br/>
		</header>
		<nav>
			<ul id="tabs">
				<?php
				echo '<li id="home"><a href="home.php?user='.$_GET["user"].'">Home</a></li>';
				echo '<li><a href = "#showCreate">Create</a></li>';
				echo '<li class="active">View&nbsp;All</li>';
				echo '<li><a href="delete.php?user='.$_GET["user"].'">Delete</a></li>';
				echo '<li id="profile"><a href="profile.php?user='.$_GET["user"].'">Profile</a></li>';
				echo '<li><a href="shared.php?user='.$_GET["user"].'">Shared</a></li>';
				?>
			</ul>
		</nav>
		<article id = "main">
			<section id="title">
				<h2> All Tasks</h2>
			</section>
			<section id="list">
			<?php
				$link = mysql_connect('localhost','cs4350','!@'); 
				if (!$link) { 
					die('Could not connect to MySQL: ' . mysql_error()); 
				}
				if (!mysql_select_db('todo', $link)) {
					die('Aw snap, no database named todo...');
				}
				
				$sql = 'SELECT * FROM list WHERE user="'.$_GET["user"].'"';
				$result = mysql_query($sql, $link);
				
				if (!$result) {
					die($sql);
				}
				while ($row = mysql_fetch_array($result)){
					if ($row['done']){
						echo '<input type="checkbox" checked="checked" disabled="disabled" />&nbsp&nbsp';
					} else {
						echo '<input type="checkbox" onclick="location.href=\'doneView.php?user=' . $_GET["user"] . '&task=' . $row['taskID'] .'\'">';
					}
					echo '<a href="#edit' . $row['taskID'] . '">' . $row['taskName'] . '</a>';
					echo '<br/><em style="cursor: default; font-style: normal" />'. $row['desc'] . '</em>';
					echo '<br/><br/>';
				}
				mysql_close($link);
			?>
			</section>
		</article>
		<div id="create">
			<form action = "create.php" method = "post" class = "straight">
				<label for = "taskName">Task Name:</label>
					<input class="textbox" type="text" name="taskName" required="required"/><br/><br/>
				<label for = "taskDesc">Task Descrpition:</label><br/>
					<textarea class="textbox" name="taskDesc"; rows="5" cols="30" maxlength="250" /></textarea></br>
				<div id="local" class="small"></div>
				<input type="submit" value="Create" name="create" class="button" />
				<a href = "#hideCreate" class = "closeDialog"><input type="button" value="Cancel" name="cancel" class="button" /></a>
				<?php
					echo '<input type="hidden" value=' . $_GET["user"] . ' name="username" />';
				?>
			</form>
		</div>
			
			<?php
				$sql = 'SELECT * FROM list WHERE user="'.$_GET["user"].'"';
				$taskResults = mysql_query($sql, $tasks);	
				
				if (!$taskResults) {
					die($sql);
				}
				while ($task = mysql_fetch_array($taskResults)){
					$sqlAttach = 'SELECT * FROM attach WHERE TaskID="'. $task['taskID'] .'"';
					$attachResults = mysql_query($sqlAttach, $tasks);	
					echo '<div id="task' . $task['taskID'] . '">';
					echo '<div class="map" style="height: auto;">Task Creation Location:<br/></div>';
					echo '<div id="map' . $task['taskID'] . '" class="map"></div>';
					echo '<form action="edit.php" method="post" enctype="multipart/form-data">';
					if ($task['done']){
						echo '<input type="checkbox" checked="checked" value="isDone" name="done" />';
					} else {
						echo '<input type="checkbox" value="isDone" name="done" />';
					}
					echo '<label for = "taskName">Task Name:</label>';
					echo '<input type="text" class="textbox" name="task" required="required" value="' . $task['taskName'] . '"/><br/><br/>';
					
					echo '<label for = "taskDesc">Task Descrpition:</label><br/>';
					echo '<textarea class="textbox" name="taskDesc" rows="10" cols="30" maxlength="250">'.$task['desc'].'</textarea><br/>';
					echo '<div class="attachments">';
						echo '<h3>Attachments:</h3>';
						while ($attach = mysql_fetch_array($attachResults)){
							if ($attach['isURL']){
								echo '<a href="http://' . $attach['fileName'] .'">' . $attach['fileName'] . '</a><br class="smallBR"/><br class="smallBR"/>';
							}
							else {
								echo '<a href="attach/' . $attach['fileName'] .'">' . $attach['fileName'] . '</a><br class="smallBR"/>';
							}
						}
						echo '<input class="fileAtt" type="file" class="textbox" name="attachName"/></br>';
						echo '<label for="attachURL">URL:</label>';
						echo '<input type="text" class="textbox" name="attachURL"><br/>';
						echo '<input type="submit" value="Attach" name="attach" class="button"/><br/>';
						echo '<input type="hidden" value="' . $task["taskID"] . '" name="id" />';							
					echo '</div>';
					echo '<input type="submit" value="Save" name="save" class="button" />';
					echo '&nbsp;&nbsp;<a href="#cancelEdit" class = "closeDialog"><input type="button" value="Cancel" name="cancel" class="button" /></a>';
					
					echo '<br/>';
					echo '<input type="hidden" value="' . $_GET["user"] . '" name="username" />';
					echo '<input type="hidden" value="' . $task["taskID"] . '" name="id" />';
					echo '</form>';
					
					echo '</div>';
				}
				mysql_close($link);
				
				$tasks = mysql_connect('localhost','cs4350','!@'); 
			if (!$tasks) { 
				die('Could not connect to MySQL: ' . mysql_error()); 
			}
			if (!mysql_select_db('todo', $tasks)) {
				die('Aw snap, no database named todo...');
			}
			
			$sql = 'SELECT * FROM list WHERE user="'.$_GET["user"].'"';
			$taskResult = mysql_query($sql, $tasks);	
			
			if (!$taskResult) {
				die($sql);
			}
			
			echo '<script>';
			echo 'window.onload = load();';
			echo 'function load()';
			echo '{';
			echo 'verifyUser();';
			echo 'getLocation();';
			while ($mapCall = mysql_fetch_array($taskResult)){
				echo 'var MapCoords' . $mapCall['taskID'] .' = {latitude: ' . $mapCall['lat'] . ',longitude: ' . $mapCall['long'] . '};';
				echo 'drawMap' . $mapCall['taskID'] . '(MapCoords' . $mapCall['taskID'] .');';
			}
			echo '}';
			
			$tasks = mysql_connect('localhost','cs4350','!@'); 
			if (!$tasks) { 
				die('Could not connect to MySQL: ' . mysql_error()); 
			}
			if (!mysql_select_db('todo', $tasks)) {
				die('Aw snap, no database named todo...');
			}
			
			$sql = 'SELECT * FROM list WHERE user="'.$_GET["user"].'"';
			$taskResult = mysql_query($sql, $tasks);	
			
			if (!$taskResult) {
				die($sql);
			}
			
			while ($mapCall = mysql_fetch_array($taskResult)){
				echo 'function drawMap' . $mapCall['taskID'] . '(coords){';
				
				//echo 'var latlon=coords.latitude+","+coords.longitude;';
				//echo 'var img_url="http://maps.googleapis.com/maps/api/staticmap?center="+latlon+"&zoom=14&size=400x300&sensor=false";';
				//echo 'document.getElementById("map' . $mapCall['taskID'] . '").innerHTML="<img src="+img_url+" />";';
			
				echo 'var googleLatAndLong = new google.maps.LatLng(coords.latitude, coords.longitude);';
				echo 'var mapOptions = {';
				echo 'zoom: 13,';
				echo 'center: googleLatAndLong,';
				echo 'mapTypeId: google.maps.MapTypeId.ROADMAP';
				echo '};';
				echo 'var mapDiv = document.getElementById("map' . $mapCall['taskID'] .'");';
				echo 'map = new google.maps.Map(mapDiv, mapOptions);';

				echo 'var title = "Location";';
				echo 'var content = "Created at: " + coords.latitude + ", " + coords.longitude;';
				echo 'addMarker(map, googleLatAndLong, title, content);';
			
				echo '}';
			}
			
			echo '</script>';
			?>
		</div>
	</section>
	</body>
</html>